﻿@{
    ViewBag.Title = "TestUtils";
    Layout = "~/Views/Shared/_Layout.Test.cshtml";
}

<h2>TestUtils</h2>

@CoffeeScripts.Render("~/CoffeeScripts/shared/coffee")       

<script type="text/coffeescript">
    utils = Cofe.utils

    test("Test utils.getNextToken", () -> 
        path = "abc/cde/efg"
               #01234567890
        token1 = utils.getNextToken(path, 0)    #abc
        token2a = utils.getNextToken(path, 3)     #cde
        token2b = utils.getNextToken(path, 4)     #cde
        token3 = utils.getNextToken(path, 7)     #efg
        token4 = utils.getNextToken(path, 11)    #undefined
        
        ok(token1 is "abc", "Error getNextToken token1" + token1)
        ok(token2a is "cde", "Error getNextToken token2a" + token2a)
        ok(token2b is "cde", "Error getNextToken token2b" + token2b)
        ok(token3 is "efg", "Error getNextToken token3" + token3)
        ok(token4 is undefined, "Error getNextToken token4" + token4)
    )


    test("Test utils.formatPageStr", () -> 
        pageStr1 = utils.formatPageStr(10)    #10 items
        pageStr2 = utils.formatPageStr(10, 2) #page 2
        
        ok(pageStr1 = "10,1", "Error formating pageStr1")
        ok(pageStr1 = "10,2", "Error formating pageStr2")
    )

    test("Test utils.updateParam", () -> 
        params1 = { abc : "cde" }
        params2 = undefined
        
        params1a = utils.updateParam(params1, "def", "efg")
        params2a = utils.updateParam(params2, "def", "efg")
        
        ok(params1a.abc isnt undefined, "params1a, original item removed")
        ok(params1a.def isnt undefined, "params1a, new item isnt added")
        ok(params2a isnt undefined, "params2a, construction failed")
        ok(params2a.def isnt undefined, "params2a, new item isnt added")
    )
   
    test("Test utils.formatUri", () -> 

        uri1a = unescape(utils.formatUri("http://uri", undefined))        
        uri1b = unescape(utils.formatUri("http://uri", undefined, 10))        
        uri1c = unescape(utils.formatUri("http://uri", undefined, 10, 2))        

        uri2a = unescape(utils.formatUri("http://uri", {abc : "cde", def : "efg"}))        
        uri2b = unescape(utils.formatUri("http://uri", {abc : "cde", def : "efg"}, 10))        
        uri2c = unescape(utils.formatUri("http://uri", {abc : "cde", def : "efg"}, 10, 2))        

        
        ok(uri1a is "http://uri", "uri1a construct failed: " + uri1a)
        ok(uri1b is "http://uri?page=1,10", "uri1b construct failed: " + uri1b)
        ok(uri1c is "http://uri?page=2,10", "uri1c construct failed: " + uri1c)

        ok(uri2a is "http://uri?abc=cde&def=efg", "uri2a construct failed: " + uri2a)
        ok(uri2b is "http://uri?abc=cde&def=efg&page=1,10", "uri2b construct failed: " + uri2b)
        ok(uri2c is "http://uri?abc=cde&def=efg&page=2,10", "uri2c construct failed: " + uri2c)
    )

    test("Test utils.formatPath", () -> 

        uri1a = unescape(utils.formatPath("{cofe}", undefined))        
        uri1b = unescape(utils.formatPath("{cofe}", undefined, 10))        
        uri1c = unescape(utils.formatPath("{cofe}", undefined, 10, 2))        

        uri2a = unescape(utils.formatPath("{cofe}", {abc : "cde", def : "efg"}))        
        uri2b = unescape(utils.formatPath("{cofe}", {abc : "cde", def : "efg"}, 10))        
        uri2c = unescape(utils.formatPath("{cofe}", {abc : "cde", def : "efg"}, 10, 2))        

        
        ok(uri1a is "/api/parse?path={cofe}", "uri1a construct failed: " + uri1a)
        ok(uri1b is "/api/parse?path={cofe}&page=1,10", "uri1b construct failed: " + uri1b)
        ok(uri1c is "/api/parse?path={cofe}&page=2,10", "uri1c construct failed: " + uri1c)

        #ok(uri2a is "http://uri?abc=cde&def=efg", "uri2a construct failed: " + uri2a)
        #ok(uri2b is "http://uri?abc=cde&def=efg&page=1,10", "uri2b construct failed: " + uri2b)
        #ok(uri2c is "http://uri?abc=cde&def=efg&page=2,10", "uri2c construct failed: " + uri2c)
    )


</script>
